import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/utils/auth'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    // component: HomeView
    component: () => import(/* webpackChunkName: "about" */ '../views/login')
  },
  {
    path: '/index',
    name: 'index',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    redirect: '/gzt',
    component: () =>
      import(/* webpackChunkName: "about" */ '../views/AboutView.vue'),
    children: [
      {
        path: '/home',
        name: 'home',
        component: () =>
          import(/* webpackChunkName: "about" */ '../views/home'),
        meta: {
          isNeedLogin: true,
          name: '首页'
        }
      },
      {
        path: '/ClearanceGoods',
        name: 'ClearanceGoods',
        component: () =>
          import(/* webpackChunkName: "about" */ '../views/ClearanceGoods'),
        meta: {
          isNeedLogin: true,
          name: '清仓商品'
        }
      },
      {
        path: '/LiveSelection',
        name: 'LiveSelection',
        component: () =>
          import(/* webpackChunkName: "about" */ '../views/LiveSelection'),
        meta: {
          isNeedLogin: true,
          name: '直播选品'
        }
      },
      {
        path: '/AdvertisingManagement',
        name: 'AdvertisingManagement',
        component: () =>
          import(/* webpackChunkName: "about" */ '../views/AdvertisingManagement'),
        meta: {
          isNeedLogin: true,
          name: '广告管理'
        }
      },
      {
        path: '/AdvertisingConfiguration',
        name: 'AdvertisingConfiguration',
        component: () =>
          import(/* webpackChunkName: "about" */ '../views/AdvertisingConfiguration'),
        meta: {
          isNeedLogin: true,
          name: '广告系统配置'
        }
      },
      {
        path: '/AdvertisementDetails',
        name: 'AdvertisementDetails',
        component: () =>
          import(/* webpackChunkName: "about" */ '../views/AdvertisingConfiguration/components/index.vue'),
        meta: {
          isNeedLogin: true,
          name: '广告详情'
        }
      },
      {
        path: '/HelpCenter',
        name: 'AdvertisementDetails',
        component: () =>
          import(/* webpackChunkName: "about" */ '../views/HelpCenter'),
        meta: {
          isNeedLogin: true,
          name: '帮助中心'
        }
      },

      {
        path: '/AboutUs',
        name: 'AboutUs',
        component: () =>
          import(/* webpackChunkName: "about" */ '../views/AboutUs'),
        meta: {
          isNeedLogin: true,
          name: '关于我们'
        }
      },
      {
        path: '/DataManagement',
        name: 'DataManagement',
        component: () =>
          import(/* webpackChunkName: "about" */ '../views/DataManagement'),
        meta: {
          isNeedLogin: true,
          name: '数据配置'
        }
      },
      {
        path: '/orderManage',
        name: 'orderManage',
        component: () =>
          import(/* webpackChunkName: "about" */ '../views/orderManage/index.vue'),
        meta: {
          isNeedLogin: true,
          name: '订单列表'
        }
      },
      {
        path: '/afterSalesManage',
        name: 'afterSalesManage',
        component: () =>
          import(/* webpackChunkName: "about" */ '../views/orderManage/afterSalesManage.vue'),
        meta: {
          isNeedLogin: true,
          name: '售后订单'
        }
      },

      {
        path: '/MembershipManagement',
        name: 'MembershipManagement',
        component: () =>
          import(/* webpackChunkName: "about" */ '../views/MembershipManagement'),
        meta: {
          isNeedLogin: true,
          name: '供应商审核'
        }
      },
      {
        path: '/MembershipManagementS',
        name: 'MembershipManagementS',
        component: () =>
          import(/* webpackChunkName: "about" */ '../views/MembershipManagementS'),
        meta: {
          isNeedLogin: true,
          name: '会员管理'
        }
      },
      {
        path: '/categoryManage',
        name: 'categoryManage',
        component: () =>
          import(/* webpackChunkName: "about" */ '../views/categoryManage/index.vue'),
        meta: {
          isNeedLogin: true,
          name: '类目管理'
        }
      },
      {
        path: '/Stats',
        name: 'Stats',
        component: () =>
          import(/* webpackChunkName: "about" */ '../views/categoryManage/components/Stats.vue'),
        meta: {
          isNeedLogin: true,
          name: '属性管理'
        }
      }

    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  // 1获取路由的元数据信息
  const { isNeedLogin } = to.meta
  // 取出登录成功之后保存的登录凭证
  const token = getToken()
  // 2判断哪些页面需要进行登录判断
  if (isNeedLogin) {
    // 证明页面需要进行登录
    if (!token) {
      next('/')
    } else {
      // 证明登陆过了
      next()
    }
  } else {
    // 证明页面不需要登录
    next() // 继续跳转到对应的页面
  }
})

export default router
